﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GONES.Tools.Common
{
    public class BarCodeEAN13
    {
        private int[] _firstCodeType = { 0, 11, 13, 19, 25, 28, 21, 22, 26 };
        private string[] _leftCode = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011", "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
        private string[] _rightCode = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
        private int _fct;
        private string barcode;
        private string barcode2;
        private int[] barchar = new int[12];
        private string stringBarcode;
        public BarCodeEAN13(string barcode)
        {
            this.barcode = barcode;
            if (barcode.Length != 12)
            {
                throw new Exception("条码必须是13位");

            }
            for (int i = 0; i < 12; i++)
            {
                barchar[i] = System.Convert.ToInt32(barcode[i].ToString()); ;
            }
            if (barchar[0] < 1)
            {
                throw new Exception("第一位导入码不能为0");

            }
            _fct = _firstCodeType[barchar[0] - 1];
            stringBarcode = "101";
            for (int i = 1; i <= 6; i++)
            {
                int _temp = (1 << +(6 - i)) & _fct;
                int _temp2 = (_temp > 0) ? 1 : 0;
                int _left = _temp2 * 10 + barchar[i];
                stringBarcode += _leftCode[_left];
            }
            stringBarcode += "01010";
            for (int i = 7; i <= 11; i++)
            {
                stringBarcode += _rightCode[barchar[i]];
            }
            int c1 = barchar[0] + barchar[2] + barchar[4] + barchar[6] + barchar[8] + barchar[10];
            int c2 = (barchar[1] + barchar[3] + barchar[5] + barchar[7] + barchar[9] + barchar[11]) * 3;
            int c = (10 - (c1 + c2) % 10) % 10;
            barcode2 = barcode + c.ToString();
            stringBarcode += _rightCode[c];
            stringBarcode += "101";
        }
        public string BarcodeString
        {
            get
            {
                return stringBarcode;
            }
        }

        public System.Drawing.Bitmap PrintBar()
        {
            System.Drawing.Bitmap img;
            System.Drawing.Graphics g;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            img = new System.Drawing.Bitmap(105, 60);

            g = System.Drawing.Graphics.FromImage(img);
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

            g.FillRectangle(brush, 0, 0, 105, 60);
            System.Drawing.Pen pb = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            System.Drawing.Pen pw = new System.Drawing.Pen(System.Drawing.Color.White, 1);
            for (int i = 0; i < stringBarcode.Length; i++)
            {
                if (stringBarcode[i] == '1')
                {
                    g.DrawLine(pb, 5 + i, 5, 5 + i, 35);
                }
            }

            g.DrawString(barcode2, (new System.Drawing.Font("arial", 8)), (new System.Drawing.SolidBrush(System.Drawing.Color.Black)), 10, 36);
            return img;
            //Color backColor = img.GetPixel(1, 1);
            //img.MakeTransparent(backColor);
            //img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            //Response.ClearContent();
            //Response.ContentType = "image/Png ";
            //Response.BinaryWrite(ms.ToArray());
            //g.Dispose();
            //img.Dispose();
            //Response.End();
        }


    }

    public class BarCode39
    {
        private char[] _code_key = { '0', '1', '2', '3','4', 
                                     '5', '6', '7', '8','9',
                                     'A', 'B', 'C', 'D', 'E', 
                                     'F', 'G', 'H', 'I', 'J', 
                                     'K', 'L', 'M', 'N', 'O', 
                                     'P', 'Q', 'R', 'S', 'T', 
                                     'U', 'V', 'W', 'X', 'Y', 
                                     'Z',  '+', '-', '*', '/', 
                                     '%', '$', '.', ' ' };
        private string[] _code_value ={  "101001101101","110100101011","101100101011","110110010101","101001101011",
                                        "110100110101","101100110101","101001011011","110100101101","101100101101",
                                        "110101001011","101101001011","110110100101","101011001011","110101100101",
                                        "101101100101","101010011011","110101001101","101101001101","101011001101",
                                        "110101010011","101101010011","110110101001","101011010011","110101101001",
                                        "101101101001","101010110011","110101011001","101101011001","101011011001",
                                        "110010101011","100110101011","110011010101","100101101011","110010110101",
                                        "100110110101","100101001001","100101011011","100101101101","100100101001",
                                        "101001001001","100100100101","110010101101","100110101101"};
        private string stringBarcode = "1001011011010";
        private string _Barcode;
        private char[] barchar;

        private int heigh = 75;

        private bool showstring = true;
        public BarCode39(string barcode)
        {
            initialize(barcode);
        }

        public BarCode39(string barcode, int heigh, bool showstring)
        {
            this.heigh = heigh;
            this.showstring = showstring;
            initialize(barcode);

        }

        private void initialize(string barcode)
        {
            _Barcode = barcode;
            int len = _Barcode.Length;
            if (len == 0)
            {
                throw new Exception("条码必须是0位");
            }
            barchar = new char[len];
            char temp;
            int pos;
            for (int i = 0; i < len; i++)
            {
                temp = _Barcode[i];
                pos = Array.IndexOf<char>(_code_key, temp);
                if (pos > -1)
                    stringBarcode = stringBarcode + _code_value[pos] + "0";
                else
                    stringBarcode = stringBarcode + _code_value[0] + "0";

            }
            stringBarcode = stringBarcode + "100101101101";
        }
        public string BarcodeString
        {
            get
            {
                return stringBarcode;
            }
            set
            {
                stringBarcode = value;
            }
        }

        public void getBarcodeString()
        {
            return;
        }

        public int Height
        {
            get { return heigh; }
            set { heigh = value; }
        }
        public System.Drawing.Bitmap PrintBar()
        {

            int h = heigh;
            if (showstring)
            {
                h = h + 16;
            }
            System.Drawing.Bitmap img;
            System.Drawing.Graphics g;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            img = new System.Drawing.Bitmap(stringBarcode.Length + 10, h);

            g = System.Drawing.Graphics.FromImage(img);
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

            g.FillRectangle(brush, 0, 0, stringBarcode.Length + 10, 100);
            System.Drawing.Pen pb = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            System.Drawing.Pen pw = new System.Drawing.Pen(System.Drawing.Color.White, 1);
            for (int i = 0; i < stringBarcode.Length; i++)
            {
                if (stringBarcode[i] == '1')
                {
                    g.DrawLine(pb, 5 + i, 0, 5 + i, heigh);
                }
            }
            if (showstring)
                g.DrawString("No." + _Barcode, (new System.Drawing.Font("arial", 8)), (new System.Drawing.SolidBrush(System.Drawing.Color.Black)), 5, heigh + 1);
            return img;
            //Color backColor = img.GetPixel(1, 1);
            //img.MakeTransparent(backColor);
            //img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            //Response.ClearContent();
            //Response.ContentType = "image/Png ";
            //Response.BinaryWrite(ms.ToArray());
            //g.Dispose();
            //img.Dispose();
            //Response.End();

        }


    }

    public class BarCode128C
    {
        private string _barcodeValue;
        private bool _ischeck;
        private string _barcode;
        private int _heigh = 20;
        private bool _showstring;
        private string[] _bars ={"11011001100","11001101100","11001100110","10010011000","10010001100",
                                "10001001100","10011001000","10011000100","10001100100","11001001000",
                                "11001000100","11000100100","10110011100","10011011100","10011001110",
                                "10111001100","10011101100","10011100110","11001110010","11001011100",
                                "11001001110","11011100100","11001110100","11101101110","11101001100",
                                "11100101100","11100100110","11101100100","11100110100","11100110010",
                                "11011011000","11011000110","11000110110","10100011000","10001011000",
                                "10001000110","10110001000","10001101000","10001100010","11010001000",
                                "11000101000","11000100010","10110111000","10110001110","10001101110",
                                "10111011000","10111000110","10001110110","11101110110","11010001110",
                                "11000101110","11011101000","11011100010","11011101110","11101011000",
                                "11101000110","11100010110","11101101000","11101100010","11100011010",
                                "11101111010","11001000010","11110001010","10100110000","10100001100",
                                "10010110000","10010000110","10000101100","10000100110","10110010000",
                                "10110000100","10011010000","10011000010","10000110100","10000110010",
                                "11000010010","11001010000","11110111010","11000010100","10001111010",
                                "10100111100","10010111100","10010011110","10111100100","10011110100",
                                "10011110010","11110100100","11110010100","11110010010","11011011110",
                                "11011110110","11110110110","10101111000","10100011110","10001011110",
                                "10111101000","10111100010","11110101000","11110100010","10111011110",
                                "10111101110","11101011110","11110101110","11010000100","11010010000",
                                "11010011100","1100011101011"};

        public BarCode128C(string barcode)
        {
            long t;
            if (!long.TryParse(barcode, out t))
                throw new Exception("格式不对！");
            if (barcode.Length % 2 == 0)
                _barcodeValue = barcode;
            else
                _barcodeValue = "0" + barcode;
        }
        public BarCode128C(string barcode, bool ischeck)
        {
            long t;
            if (!long.TryParse(barcode, out t))
                throw new Exception("格式不对！");
            if (barcode.Length % 2 == 0)
                _barcodeValue = barcode;
            else
                _barcodeValue = "0" + barcode;
            _ischeck = ischeck;
        }
        public bool IsCheck
        {
            get { return _ischeck; }
            set { _ischeck = value; }
        }
        public bool ShowString
        {
            get { return _showstring; }
            set { _showstring = value; }
        }
        public string BarValue
        {
            get { return _barcodeValue; }
            set { _barcodeValue = value; }
        }

        public int Height
        {
            get { return _heigh; }
            set { _heigh = value; }
        }
        private bool _check()
        {
            if (_barcodeValue.Length % 2 != 0)
            {
                return false;
            }

            return System.Text.RegularExpressions.Regex.IsMatch(_barcodeValue, @"\d+");
        }

        private void _getBarcode()
        {
            _barcode = "";
            if (!_check()) return;
            int len = _barcodeValue.Length / 2;
            _barcode = "11010011100";
            int _posvalue;
            int _checkvalue = 105;
            for (int i = 0; i < len; i++)
            {
                _posvalue = int.Parse(_barcodeValue.Substring(i * 2, 2));
                _barcode += _bars[_posvalue];
                _checkvalue += _posvalue * (i + 1);
            }
            _checkvalue = _checkvalue % 103;
            _barcode += _bars[_checkvalue];
            _barcode += "1100011101011";
        }

        public System.Drawing.Bitmap PrintBar()
        {
            _getBarcode();
            if (string.IsNullOrEmpty(_barcode))
            {
                return null;
            }
            int h = _heigh;
            if (_showstring)
            {
                h = h + 16;
            }
            System.Drawing.Bitmap img;
            System.Drawing.Graphics g;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            img = new System.Drawing.Bitmap(_barcode.Length + 10, h);

            g = System.Drawing.Graphics.FromImage(img);
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

            g.FillRectangle(brush, 0, 0, _barcode.Length + 10, 100);
            System.Drawing.Pen pb = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            System.Drawing.Pen pw = new System.Drawing.Pen(System.Drawing.Color.White, 1);
            for (int i = 0; i < _barcode.Length; i++)
            {
                if (_barcode[i] == '1')
                {
                    g.DrawLine(pb, 5 + i, 0, 5 + i, _heigh);
                }
            }
            if (_showstring)
                g.DrawString("No." + _barcodeValue, (new System.Drawing.Font("arial", 8)), (new System.Drawing.SolidBrush(System.Drawing.Color.Black)), 5, _heigh + 1);
            return img;
            //Color backColor = img.GetPixel(1, 1);
            //img.MakeTransparent(backColor);
            //img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            //Response.ClearContent();
            //Response.ContentType = "image/Png ";
            //Response.BinaryWrite(ms.ToArray());
            //g.Dispose();
            //img.Dispose();
            //Response.End();

        }
    }
}
